package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.selectapply

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.hitPointCameroon
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.fullapply.FullApplyCameroonActivity
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.subview.HotlineCameroonDialog
import com.helasasa.maxfunds.lineofcredit.cashloan.databinding.ActivityCameroonSelectApplyBinding

class SelectApplyCameroonActivity : AppCompatActivity() {

    private lateinit var selectApplyViewModel: SelectApplyCameroonViewModel
    private lateinit var binding: ActivityCameroonSelectApplyBinding

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 数据模型
        selectApplyViewModel = ViewModelProvider(this)[SelectApplyCameroonViewModel::class.java]

        // 关联布局
        binding = ActivityCameroonSelectApplyBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 用到的控件
        val back = binding.navbarLeft
        val hotline = binding.navbarRight
        val quickBg = binding.quickBg
        val fullBg = binding.fullBg
        val checkbox1 = binding.checkbox1
        val checkbox2 = binding.checkbox2
        val loading = binding.loading

        // 页面loading状态接口监听
        selectApplyViewModel.loadingResult.observe(this@SelectApplyCameroonActivity, Observer {
            val loadingResult = it ?: return@Observer

            if (loadingResult) {
                loading.visibility = View.VISIBLE
            } else {
                loading.visibility = View.GONE
            }
        })

        // 流程接口监听
        selectApplyViewModel.mainProcessResult.observe(this@SelectApplyCameroonActivity, Observer {
            val mainProcessResult = it ?: return@Observer

            loading.visibility = View.GONE
            if (mainProcessResult.error != null) {
                showMessage(mainProcessResult.error)
            }

            if (mainProcessResult.success != null) {
                selectApplyViewModel.nextRouter(mainProcessResult.success)
            }
        })

        // 页面需要跳转监听
        selectApplyViewModel.nextRouterResult.observe(this@SelectApplyCameroonActivity, Observer {
            val nextRouterResult = it ?: return@Observer

            loading.visibility = View.GONE
            startNextActivity(nextRouterResult.nextPage)
        })

        // 点击申请
        back.setOnClickListener {
            finish()
        }

        // 点击客服
        hotline.setOnClickListener {
            // 客服弹框
            HotlineCameroonDialog().showNow(supportFragmentManager, "hotline")
            hitPointCameroon("SHOW_HOTLINE")
        }

        // 点击full
        fullBg.setOnClickListener {
            hitPointCameroon("SELECT_FULL_APPLY")

            checkbox1.setImageResource(R.drawable.pic_select_apply_uncheck)
            checkbox2.setImageResource(R.drawable.pic_select_apply_checked)
            quickBg.setBackgroundResource(R.drawable.bg_select_apply_uncheck)
            fullBg.setBackgroundResource(R.drawable.bg_select_apply_checked)

            val intent = Intent(this@SelectApplyCameroonActivity, FullApplyCameroonActivity::class.java)
            startActivity(intent)
        }

        // 点击quickly
        quickBg.setOnClickListener {
            hitPointCameroon("SELECT_QUICK_APPLY")

            checkbox1.setImageResource(R.drawable.pic_select_apply_checked)
            checkbox2.setImageResource(R.drawable.pic_select_apply_uncheck)
            quickBg.setBackgroundResource(R.drawable.bg_select_apply_checked)
            fullBg.setBackgroundResource(R.drawable.bg_select_apply_uncheck)

            selectApplyViewModel.mainProcess()
        }
    }

    private fun showMessage(message: String) {
        Toast.makeText(applicationContext, message, Toast.LENGTH_SHORT).show()
    }

    override fun onResume() {
        super.onResume()
        hitPointCameroon(type = this::class.java.simpleName, isPageStart = true)
    }

    override fun onPause() {
        super.onPause()
        hitPointCameroon(type = this::class.java.simpleName, isPageEnd = true)
    }
}

fun Context.startNextActivity(
    activity: Class<out AppCompatActivity>? = null,
    params: HashMap<String, String> = HashMap()
) {
    val nextActivity = activity ?: return
    try {
        val intent = Intent(this, nextActivity)
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
        params.keys.forEach { gKey ->
            intent.putExtra(gKey, params[gKey])
        }
        startActivity(intent)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}